﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Util;
using System.IO;

public partial class FrontEnd_Dialog_UploadPhoto : System.Web.UI.Page
{
    protected BLO.Album albums;
    protected String userID;
    protected int albumID;
    public string message = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        uploadPicture();
    }
    protected void uploadPicture()
    {
        if (Session["LoginUser"] == null) return;
        else userID = Session["LoginUser"].ToString();
       // String kind = Request.Form["hddKindSubmit"];
        String kind = Request.Form.Get("hddKindSubmit");
        if (kind == "upload")
        {
            //Insert album if haven't
            if (albums == null) albums = new BLO.Album();
            String albumName = Request.Form.Get("txtAlbumName");
            String folderAlbum = Server.MapPath(ServerPath.UserData + "/" + userID + "/Photo");
            folderAlbum += "/" + albumName;
            if (albumName != "")
            {
                if (!Directory.Exists(folderAlbum))
                {
                    Directory.CreateDirectory(folderAlbum);
                    DTO.Album item = new DTO.Album();
                    item.Name = albumName;
                    item.UserID = userID;
                    albums.Info = item;
                    albumID = albums.Insert();
                    Directory.CreateDirectory(folderAlbum);
                }
            }
            else
            {
                albumID = int.Parse(Request.Form.Get("sltAlbum"));
                albumName = BLO.Album.GetNameAlbum(albumID.ToString());              
                folderAlbum += "/" + albumName;
                if (!Directory.Exists(folderAlbum))
                {
                    Directory.CreateDirectory(folderAlbum);
                    
                }
            }
            string photoName = Request.Form["txtPictureName"];
            string Duoi=FunctionSupport.LayDuoiFile(FileUpload1.FileName);
            if (photoName == "") { photoName = FileUpload1.FileName; Duoi = ""; }
            photoName += Duoi;
            BLO.Photo photo = new BLO.Photo();
            DTO.Photo itemPhoto = new DTO.Photo();
            itemPhoto.Name = photoName;
            itemPhoto.AlbumID = albumID;
            itemPhoto.UserID = userID;
            itemPhoto.Description = Request.Form["txtDescription"];
            itemPhoto.Link = "Photo/"+albumName+"/"+photoName;
            photo.Info = itemPhoto;
            int result = photo.Insert();
            if (result > -1)
            {
                FileUpload1.SaveAs(folderAlbum + "/" +photoName);
                message = "Upload thành công";
            }
        }
    }
}
